<template>
    <div class="table-box">
        <ProTable ref="proTable" :requestApi="api.getList" :columns="columns">
            <!-- 表格 header 按钮 -->
            <template #tableHeader="scope">
                <el-button type="primary" :icon="CirclePlus" @click="openOptionDialog('新增')">新增</el-button>
                <el-button type="danger" :icon="Delete" plain :disabled="!scope.isSelected" @click="batchDelete(scope.ids)">
                    批量删除
                </el-button>
            </template>
            <template #zt="scope">
                <!-- 如果插槽的值为 el-switch，第一次加载会默认触发 switch 的 @change 方法，所有在外层包一个盒子，点击触发盒子 click 方法 -->
                <div @click="handleChange({ id: scope.row.id, zt: scope.row.zt })">
                    <el-switch v-model="scope.row.zt" :active-text="scope.row.zt == '1' ? '启用' : '停用'" active-value="1"
                        inactive-value="0" />
                </div>
            </template>
            <!-- 表格操作 -->
            <template #operation="scope">
                <el-button type="primary" link :icon="EditPen" @click="openOptionDialog('编辑', scope.row)">编辑</el-button>
                <el-button type="primary" link :icon="Delete" @click="onRemove(scope.row)">删除</el-button>
            </template>
        </ProTable>
        <FormDialog ref="formDialogRef"></FormDialog>
    </div>
</template>

<script setup lang="tsx" name="operationLogConfig">

import { ref, reactive } from "vue";
import { CirclePlus, Delete, EditPen, Discount } from "@element-plus/icons-vue";

import { useHandleData } from "/@/hooks/useHandleData";

import FormDialog from "./components/FormDialog.vue";
import { useMessage } from '/@/hooks/useMessage';
import * as api from "../../../api/system/operationLogConfig";
import columnsFunction from './columns';
import ProTable from "/@/components/ProTable/index.vue";

// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref();

// 获取表头信息
const columns = columnsFunction();

const batchDelete = async (ids: string[]) => {
    await useHandleData(api.remove, { ids }, "删除选择的配置信息");
    proTable.value.refresh();
};

// 删除用户信息
const onRemove = async (params: any) => {
    await useHandleData(api.remove, { id: params.id }, '删除选择的配置信息');
    proTable.value.refresh();
};

const handleChange = async (params: any) => {
    useMessage(api.edit, params);

}

// 打开 dialog(新增、查看、编辑)
interface FormDialogExpose {
    acceptParams: (params: any) => void;
}
const formDialogRef = ref<FormDialogExpose>();

const openOptionDialog = (title: string, rowData = {}) => {
    let params = {
        title: title,
        rowData,
        isView: title === "查看" ? true : false,
        apiUrl: title === "新增" ? api.add : title === "编辑" ? api.edit : "",
        getTableList: proTable.value.refresh
    };
    formDialogRef.value!.acceptParams(params);
};
</script>
